package df.bean.process;

import df.applet.form.PaymentMonthlyFrm;
import df.bean.db.conn.DBConnection;
import df.bean.db.table.Batch;
import df.bean.db.table.PaymentMonthly;
import df.bean.obj.util.DialogBox;
import df.bean.obj.util.JDate;
import df.bean.obj.util.Variables;

public class ProcessPaymentMonthly extends Process {
    String YYYY = "";
    String MM = "";
    String DD = "";
    PaymentMonthly pm = null;

    public ProcessPaymentMonthly(DBConnection conn) {
        super(conn);
        this.setDBConnection(conn);
    }
    public ProcessPaymentMonthly() {
        super();
    }

    @Override
    protected void finalize() {
    }

//    @Override
//    public boolean run(String YYYY, String MM, String hospitalCode) {
//        if (this.getDBConnection().IsClosed()) {  
//            if (!this.getDBConnection().Connect()) { DialogBox.ShowError(" Error "); return false;}
//        }
//        
//        this.getDBConnection().beginTrans();
//        
//        result = this.Calculate(YYYY, MM, hospitalCode);
//        if (this.result) {
//            this.getDBConnection().commitTrans();
//        } else {
//            this.getDBConnection().rollBackTrans();
//        }         
//        
//        return result;
//    }
//    
//    @Override
//    public boolean Calculate(String YYYY, String MM, String hospitalCode) {
//        boolean ret = false;
//
//        PaymentMonthly pm = new PaymentMonthly(this.getDBConnection());
//        if (pm.insertToPaymentSummmary(hospitalCode)>-1) { ret = true; }
//        pm = null;
//
//        return ret;
//    } 
    
    public boolean runForSalary(String hospitalCode, String payDate) {
        if (this.getDBConnection().IsClosed()) {  
            if (!this.getDBConnection().Connect()) { DialogBox.ShowError(" Error "); return false;}
        }
        
        this.getDBConnection().beginTrans();
        
        result = this.CalculateForSalary(hospitalCode, payDate);
        if (this.result) {
            this.getDBConnection().commitTrans();
        } else {
            this.getDBConnection().rollBackTrans();
        }         
        
        return result;
    }
    
    public boolean CalculateForSalary(String hospitalCode, String payDate) {
        boolean ret = false;
        PaymentMonthly pm = new PaymentMonthly(this.getDBConnection());
        if (pm.insertToPaymentSummmaryForSalary(hospitalCode, payDate)>-1) { ret = true; }
        pm = null;

        return ret;
    } 
    
    ////////////////////////// By doctor //////////////////////////
//    public boolean run(String YYYY, String MM, String hospitalCode, String doctorCode) {
//        if (this.getDBConnection().IsClosed()) {  
//            if (!this.getDBConnection().Connect()) { DialogBox.ShowError(" Error "); return false;}
//        }
//        
//        this.getDBConnection().beginTrans();
//        
//        result = this.Calculate(YYYY, MM, hospitalCode, doctorCode);
//        if (this.result) {
//            this.getDBConnection().commitTrans();
//        } else {
//            this.getDBConnection().rollBackTrans();
//        }         
//        return result;
//    }
    
//    public boolean Calculate(String YYYY, String MM, String hospitalCode, String doctorCode) {
//        boolean ret = false;
//
//        PaymentMonthly pm = new PaymentMonthly(this.getDBConnection());
//        if (pm.insertToPaymentSummmary(hospitalCode, doctorCode)>-1) { ret = true; }
//        pm = null;
//
//        return ret;
//    } 
        
    
    public boolean runForSalary(String hospitalCode, String payDate, String doctorCode) {
        if (this.getDBConnection().IsClosed()) {  
            if (!this.getDBConnection().Connect()) { return false; }//DialogBox.ShowError(" Error "); return false;}
        }
        
        this.getDBConnection().beginTrans();
        
        result = this.CalculateForSalary(hospitalCode, payDate, doctorCode);
        if (this.result) {
            this.getDBConnection().commitTrans();
        } else {
            this.getDBConnection().rollBackTrans();
        }         
        
        return result;
    }
    
    public boolean CalculateForSalary(String hospitalCode, String payDate, String doctorCode) {
        boolean ret = false;
        PaymentMonthly pm = new PaymentMonthly(this.getDBConnection());
        if (pm.insertToPaymentSummmaryForSalary(hospitalCode, payDate, doctorCode)>-1) { ret = true; }
        pm = null;

        return ret;
    } 
    // Used here
    
    @Override
    public void run() {
        String lstCode = "";
        boolean ret = true;
        try {
            if (working) { 
                values.clear();
                values.add("---- Start ----");
                values.add(JDate.getDate());
                values.add(JDate.getTime());
                this.calculatorFrm.setTableValue(values);
                this.calculatorFrm.setRun();

                pm = new PaymentMonthly(this.getDBConnection());
                pm.resultSetForInsert = null;
                pm.statementForQuerySalary = null;
                pm.statementForQuery = null;
                pm.statementForInsert = null;
//                pm = null;
                

                // Eash Hospital
                this.calculatorFrm.getLstHospitalCode().setSelectedIndex(0);
                this.calculatorFrm.setHospitalCode(this.calculatorFrm.getLstHospitalCode().getSelectedValue().toString());
                this.calculatorFrm.addDoctorGroupToList(Variables.getHospitalCode());

                // create batch // don't remove because we need to use static variable such as YYYY and MM
                Batch batch = new Batch(Variables.getHospitalCode(), this.getDBConnection());
                DD = ((PaymentMonthlyFrm)this.calculatorFrm).getDD();
                MM = ((PaymentMonthlyFrm)this.calculatorFrm).getMM();
                YYYY = ((PaymentMonthlyFrm)this.calculatorFrm).getYYYY();
                
                this.getDBConnection().beginTrans();
                // Eash Doctor
                for (int j=0; j<this.calculatorFrm.getLstDoctorGroupCode().getModel().getSize(); j++) {
                    this.calculatorFrm.getLstDoctorGroupCode().setSelectedIndex(j);
                    lstCode = this.calculatorFrm.getLstDoctorGroupCode().getSelectedValue().toString();
                    this.calculatorFrm.setCount((j+1) + " : " + this.calculatorFrm.getLstDoctorGroupCode().getModel().getSize());

                    if (!Calculate(Variables.getHospitalCode(), lstCode, YYYY+MM+DD)) {
                        running = false; ret = false;
                    }
                    
                    if (!running) break;
                    if (!working) break;
                } 
                batch = null;

                working = false;
                values.clear();
                values.add("==== Stop ====");
                values.add(JDate.getDate());
                values.add(JDate.getTime());
                this.calculatorFrm.setTableValue(values);
                values = null; 
            }
        } catch (Exception e) {
            // TODO
            running = false;
            DialogBox.ShowError("Error in Process.Run() \n" + lstCode + "\n" + e.getMessage());
            System.out.print(e.getMessage());
            this.calculatorFrm.stopProcess();
        } finally {   
            if (running == true) { this.getDBConnection().commitTrans(); }
            if (running == false) { this.getDBConnection().rollBackTrans(); }
            if (running == true) { DialogBox.ShowInfo("     Completed !!!!   ");    }
            if (ret == false) { DialogBox.ShowError("      Fail !!!!  "); }
            this.calculatorFrm.setStop();
            this.calculatorFrm.stopProcess();
        }

    }
    
    @Override
    public boolean Calculate(String hospitalCode, String doctorCode, String payDate) {
        boolean ret = false;
        values.clear();
        values.add(JDate.getTime());
        values.add(doctorCode);
            
//        PaymentMonthly pm = new PaymentMonthly(this.getDBConnection());
//        pm = new PaymentMonthly(this.getDBConnection());
        if (pm.insertToPaymentSummmary(hospitalCode, payDate, doctorCode)>-1) {
            values.add("Ok");
            ret = true;    
        } 
        else {  
            values.add("Fail !!!!");
            ret = false;   
        }
        
        if (this.calculatorFrm != null) this.calculatorFrm.setTableValue(values);
//        pm = null;
        return ret;
    } 
}
